<% admin_breadcrumb(plural_resource_name(Spree::Order)) %>


<% content_for :page_actions do %>
  <li>
    <%= button_link_to Spree.t(:new_order), new_admin_order_url, :id => 'admin_new_order' %>
  </li>
<% end if can? :edit, Spree::Order.new %>

<% content_for :table_filter_title do %>
  <%= Spree.t(:search) %>
<% end %>

<% content_for :table_filter do %>
  <div data-hook="admin_orders_index_search">
    <%= search_form_for [:admin, @search] do |f| %>
      <div class="row">
        <div class="field-block col-xs-3">
          <div class="date-range-filter field">
            <%= label_tag :q_created_at_gt, Spree.t(:date_range) %>
            <div class="date-range-fields">
              <%= f.text_field :created_at_gt, :class => 'datepicker datepicker-from', :value => params[:q][:created_at_gt], :placeholder => Spree.t(:start) %>

              <span class="range-divider">
                <i class="fa fa-arrow-right"></i>
              </span>

              <%= f.text_field :created_at_lt, :class => 'datepicker datepicker-to', :value => params[:q][:created_at_lt], :placeholder => Spree.t(:stop) %>
            </div>
          </div>

          <div class="field">
            <%= label_tag :q_state_eq, Spree.t(:status) %>
            <%= f.select :state_eq, Spree::Order.state_machines[:state].states.collect {|s| [Spree.t("order_state.#{s.name}"), s.value]}, {:include_blank => true}, :class => 'select2' %>
          </div>

          <div class="field">
            <%= label_tag nil, Spree.t(:promotion) %>
            <%= f.text_field :promotions_codes_value_cont, :size => 25 %>
          </div>

          <div class="field">
            <%= label_tag nil, Spree.t(:shipment_number) %>
            <%= f.text_field :shipments_number_cont %>
          </div>

        </div>

        <div class="col-xs-6">
          <div class="row">
            <div class="col-xs-6">
              <div class="field">
                <%= label_tag :q_number_cont, Spree.t(:order_number, :number => '') %>
                <%= f.text_field :number_cont %>
              </div>

              <div class="field">
                <%= label_tag :q_email_cont, Spree.t(:email) %>
                <%= f.text_field :email_cont %>
              </div>
            </div>

            <div class="col-xs-6">
              <div class="field">
                <%= label_tag :q_bill_address_firstname_start, Spree.t(:first_name_begins_with) %>
                <%= f.text_field :bill_address_firstname_start, :size => 25 %>
              </div>
              <div class="field">
                <%= label_tag :q_bill_address_lastname_start, Spree.t(:last_name_begins_with) %>
                <%= f.text_field :bill_address_lastname_start, :size => 25%>
              </div>
            </div>

            <div class="col-xs-12">
              <div class="field" data-hook="sku-select">
                <%= label_tag :q_line_items_variant_id_in, Spree.t(:variant) %>
                <%= f.text_field :line_items_variant_id_in, class: "variant_autocomplete fullwidth" %>
              </div>
            </div>
          </div>
        </div>

        <div class="col-xs-3">
          <% if Spree::Store.count > 1 %>
            <div class="field">
              <%= label_tag nil, Spree.t(:store) %>
              <%= f.select :store_id_eq, Spree::Store.all.map { |s| [s.name, s.id] }, { include_blank: true }, { class: "select2" } %>
            </div>
          <% end %>

          <div class="field checkbox">
            <label>
              <%= f.check_box :completed_at_not_null, {:checked => @show_only_completed}, '1', '0' %>
              <%= Spree.t(:show_only_complete_orders) %>
            </label>
          </div>
        </div>

      </div>

      <div class="clearfix"></div>

      <div class="actions filter-actions">
        <div data-hook="admin_orders_index_search_buttons">
          <%= button Spree.t(:filter_results) %>
        </div>
      </div>
    <% end %>
  </div>
<% end %>

<%= paginate @orders, theme: "solidus_admin" %>

<% if @orders.any? %>
  <table class="index" id="listing_orders" data-hook>
    <colgroup>
       <col style="width: 13%;">
       <col style="width: 10%;">
       <col style="width: 10%;">
       <col style="width: 12%;">
       <% if Spree::Order.checkout_step_names.include?(:delivery) %>
         <col style="width: 12%;">
       <% end %>
       <col style="width: 25%;">
       <col style="width: 10%;">
       <col style="width: 8%;">
    </colgroup>
    <thead>
      <tr data-hook="admin_orders_index_headers">
        <% if @show_only_completed %>
          <th><%= sort_link @search, :completed_at %></th>
        <% else %>
          <th><%= sort_link @search, :created_at %></th>
        <% end %>
        <th><%= sort_link @search, :number %></th>
        <th><%= sort_link @search, :state %></th>
        <th><%= sort_link @search, :payment_state %></th>
        <% if Spree::Order.checkout_step_names.include?(:delivery) %>
          <th><%= sort_link @search, :shipment_state %></th>
        <% end %>
        <th><%= sort_link @search, :email %></th>
        <th><%= sort_link @search, :total %></th>
        <th data-hook="admin_orders_index_header_actions" class="actions"></th>
      </tr>
    </thead>
    <tbody>
    <% @orders.each do |order| %>
      <tr data-hook="admin_orders_index_rows" class="state-<%= order.state.downcase %> <%= cycle('odd', 'even') %>">
        <td class="align-center"><%= l (@show_only_completed ? order.completed_at : order.created_at).to_date %></td>
        <td class="align-center"><%= link_to order.number, edit_admin_order_path(order) %></td>
        <td class="align-center"><span class="state <%= order.state.downcase %>"><%= Spree.t("order_state.#{order.state.downcase}") %></span></td>
        <td class="align-center"><span class="state <%= order.payment_state %>"><%= link_to Spree.t("payment_states.#{order.payment_state}"), admin_order_payments_path(order) if order.payment_state %></span></td>
          <% if Spree::Order.checkout_step_names.include?(:delivery) %>
            <td class="align-center"><span class="state <%= order.shipment_state %>"><%= Spree.t("shipment_states.#{order.shipment_state}") if order.shipment_state %></span></td>
          <% end %>
        <td>
          <% if order.user %>
            <%= link_to order.email, edit_admin_user_path(order.user) %>
          <% else %>
            <%= mail_to order.email %>
          <% end %>
        </td>
        <td class="align-center"><%= order.display_total.to_html %></td>
        <td class='actions align-center' data-hook="admin_orders_index_row_actions">
          <%= link_to_edit_url edit_admin_order_path(order), :title => "admin_edit_#{dom_id(order)}", :no_text => true %>
        </td>
      </tr>
    <% end %>
    </tbody>
  </table>
<% else %>
  <div class="col-xs-9 no-objects-found">
    <%= render 'spree/admin/shared/no_objects_found',
                 resource: Spree::Order,
                 new_resource_url: spree.new_admin_order_path %>
  </div>
<% end %>

<%= paginate @orders, theme: "solidus_admin" %>
